<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>CSSMediaRule functions test</title>
        <style id="style1">
            @media all { .test { color: green; } }
        </style>
        <script>
            function log(message) {
                var item = document.createElement("li");
                item.appendChild(document.createTextNode(message));
                document.getElementById("console").appendChild(item);
            }

            function test() {
                if (window.layoutTestController)
                    layoutTestController.dumpAsText();

                var styleSheet = document.getElementById('style1').sheet;
                var mediaRule = styleSheet.cssRules[0];

                // CSSMediaRule.insertRule(rule, index) tests

                // Test that insertRule works.
                try {
                    var index = mediaRule.insertRule(".test2 { color: blue; }", mediaRule.cssRules.length);
                    log("PASS: No exception raised! New rule inserted successfully.");
                } catch (e) {
                    log("FAIL: no exception should have been thrown! Type of thrown exception was: " + e);
                }

                // Test that insertRule raises an exception for indexes greater than the length of the list.
                try {
                    var index = mediaRule.insertRule("p {color: red; }", mediaRule.cssRules.length + 1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'Error: INDEX_SIZE_ERR: DOM Exception 1'.");
                }

                // Test that insertRule raises an exception for indexes less than 0.
                try {
                    var index = mediaRule.insertRule("p {color: red; }", -1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'Error: INDEX_SIZE_ERR: DOM Exception 1'.");
                }

                // Test that insertRule raises an exception for malformed rules.
                try {
                    var index = mediaRule.insertRule("badbeef }{", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been Error: SYNTAX_ERR: DOM Exception 12!");
                }

                // Test that insertRule raises an exception for illegally placed rules.
                try {
                    // ImportRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@import url(sheet.css);", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 3)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e.code + " was thrown, should of been Error: HIERARCHY_REQUEST_ERR: DOM Exception 3!");
                }
                try {
                    // CharsetRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@charset \"ISO-8859-1\";", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    // FIXME: this should throw a HIERARCHY_REQUEST_ERR, not a SYNTAX_ERR.
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been Error: SYNTAX_ERR: DOM Exception 12!");
                }
                try {
                    // Nested MediaRule illegal inside a MediaRule.
                    var index = mediaRule.insertRule("@media screen { p { color: red; } };", mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    // FIXME: this should throw a HIERARCHY_REQUEST_ERR, not a SYNTAX_ERR.
                    if (e.code == 12)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been Error: SYNTAX_ERR: DOM Exception 12!");
                }


                // CSSMediaRule.deleteRule(index) tests

                // Test that deleteRule works.
                try {
                    mediaRule.deleteRule(mediaRule.cssRules.length - 1);
                    log("PASS: No exception raised! Rule at position 'length - 1' deleted successfully.");
                } catch (e) {
                    log("FAIL: no exception should have been thrown! Type of thrown exception was: " + e);
                }

                // Test that deleteRule raises an exception for specified indexes not corresponding to a 
                // rule in the media rule list.
                try {
                    mediaRule.deleteRule(mediaRule.cssRules.length);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'Error: INDEX_SIZE_ERR: DOM Exception 1'.");
                }
                try {
                    mediaRule.deleteRule(-1);
                    log("FAIL: an exception should have been thrown!");
                } catch (e) {
                    if (e.code == 1)
                        log("PASS: Exception raised successfully. Type: " + e);
                    else
                        log("FAIL: wrong exception type thrown. " + e + " was thrown, should of been 'Error: INDEX_SIZE_ERR: DOM Exception 1'.");
                }
            }
        </script>
    </head>
    <body onload="test();">
        <p>This tests the insertRule(rule, index) and deleteRule(index) methods of the CSSMediaRule interface.  It has passed if
            all of the output below begins with the text "PASS".  
        <ol id="console">
        </ol>
    </body>
</html>
